package com.crazy.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.NoSuchMessageException;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Locale;

/**
 * @author panyonghui
 * @describe 应用辅助类
 * @date 2022/2/24 11:37
 */
@Slf4j
@Component("applicationHelper")
public class ApplicationHelper implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationHelper.applicationContext = applicationContext;
    }

    /**
     * @describe 获取bean对象
     * @auther crazy_cat
     * @date 2022/2/24 11:43
     * @param beanName
     * @return T
     * @modify
     */
    public static <T> T getBean(String beanName){
        return (T)applicationContext.getBean(beanName);
    }

    /**
     * @describe 获取指定类型的bean对象
     * @auther crazy_cat
     * @date 2022/2/24 11:44
     * @param beanName
     * @param clazz
     * @return T
     * @modify
     */
    public static <T> T getBean(String beanName, Class<T> clazz){
        return applicationContext.getBean(beanName, clazz);
    }

    /**
     * @describe 是否存在该bean对象
     * @auther crazy_cat
     * @date 2022/2/24 11:44
     * @param beanName
     * @return boolean
     * @modify
     */
    public static boolean containsBean(String beanName){
        return applicationContext.containsBean(beanName);
    }

    /**
     * @describe 判断bean是否是单例对象
     * @auther crazy_cat
     * @date 2022/2/24 11:45
     * @param beanName
     * @return boolean
     * @modify
     */
    public static boolean isSingleton(String beanName){
        return applicationContext.isSingleton(beanName);
    }

    /**
     * @describe 获取对象类型
     * @auther crazy_cat
     * @date 2022/2/24 11:48
     * @param beanName
     * @return java.lang.Class<? extends java.lang.Object>
     * @modify
     */
    public static Class<? extends Object> getType(String beanName){
        return applicationContext.getType(beanName);
    }

    /**
     * @describe 获取环境对象
     * @auther crazy_cat
     * @date 2022/2/24 11:55
     * @param
     * @return org.springframework.core.env.Environment
     * @modify
     */
    public static Environment getEnvironment(){
        return applicationContext.getEnvironment();
    }

    /**
     * @describe 获取配置
     * @auther crazy_cat
     * @date 2022/2/24 11:52
     * @param keyName
     * @return java.lang.String
     * @modify
     */
    public static String getProperties(String keyName){
        return applicationContext.getEnvironment().getProperty(keyName);
    }

    /**
     * @describe 获取配置
     * @auther crazy_cat
     * @date 2022/2/24 11:52
     * @param keyName
     * @param defaultValue
     * @return java.lang.String
     * @modify
     */
    public static String getProperties(String keyName, String defaultValue){
        return applicationContext.getEnvironment().getProperty(keyName, defaultValue);
    }

    /**
     * @describe 获取指定类型配置
     * @auther crazy_cat
     * @date 2022/2/24 11:53
     * @param keyName
     * @param clazz
     * @return T
     * @modify
     */
    public static <T> T getProperties(String keyName, Class<T> clazz){
        return applicationContext.getEnvironment().getProperty(keyName, clazz);
    }

    /**
     * @describe 获取消息配置
     * @auther crazy_cat
     * @date 2022/2/24 14:30
     * @param messageKey
     * @param args
     * @return java.lang.String
     * @modify
     */
    public static String getMessage(String messageKey, Object[] args){
        try {
            return applicationContext.getMessage(messageKey, args, Locale.SIMPLIFIED_CHINESE);
        } catch (NoSuchMessageException e){
            log.error(messageKey + "未配置", e);
        }
        return null;
    }

    /**获取消息配置
     * @describe
     * @auther crazy_cat
     * @date 2022/2/24 14:31
     * @param messageKey
     * @param args
     * @param defaultValue
     * @return java.lang.String
     * @modify
     */
    public static String getMessage(String messageKey, Object[] args, String defaultValue){
        try {
            return applicationContext.getMessage(messageKey, args, defaultValue, Locale.SIMPLIFIED_CHINESE);
        } catch (NoSuchMessageException e){
            log.error(messageKey + "未配置", e);
        }
        return null;
    }
}
